var tid = localStorage.getItem("m20101tid");
if(tid==null)
mui.alert('未正确跳转','提示','确认',function (e) { mui.back();},'div');

var mr = ID("m231root");

function ID(id)
{
	return document.getElementById(id);
}
function CN(className)
{
	return document.getElementsByClassName(className);
}

function m201011Init()
{
	var tdata = {'tid':tid,'uid':localStorage.getItem("uid")};
	mui.ajax(ASYURI+'M201011.do',{
		data:tdata,
		dataType:'json',//服务器返回json格式数据
		type:'post',//HTTP请求类型
		timeout:10000,//超时时间设置为10秒；
		success:function(data){
			if(data.tag=='1')
			mui.alert('出现了未知的错误！','提示','确认',function (e){ mui.back();},'div');
			else if(data.tag=='0')
			{
				var s1 = data[1];
				cardInit(s1.B1031,s1.B1033);
				
				var s2 = data[2];
				var s2len = s2.length;
				
				var ul = mainListInit();
				
				for(mi=0;mi<s2len;mi++)
				{
					var ss2 = s2[mi];
					console.log(ss2);
					var il = mainItemInit(ss2.A1011,ss2.A1051,ss2.A1010);
					ul.appendChild(il);
				}
				mr.appendChild(ul);
			}
		},
		error:function(xhr,type,errorThrown){
			
		}
	});
}

function mainListInit()
{
/**
<ul class="mui-table-view">
	<li class="mui-table-view-cell">
		<a class="mui-navigate-right">
			Item 1
		</a>
	</li>
</ul>
 */
	var ul = document.createElement("ul");
	ul.setAttribute("class","mui-table-view");
	return ul;
}
function mainItemInit(iVal1,iVal2,iVal3)
{
	var li = document.createElement("li");
	li.setAttribute("class","mui-table-view-cell");

	var p = document.createElement("b");
	p.innerHTML = iVal1;
	p.setAttribute("onclick","QuId('"+iVal3+"')");
	li.appendChild(p);
	var sw = document.createElement("div");
	if(iVal2=='1')
	{
		var p2 = document.createElement("p")
		p2.setAttribute("style","color:green;");
		p2.setAttribute("id", iVal3);
		p2.innerHTML = "正在问答中";
		li.appendChild(p2);
		var btn = document.createElement("button");
		btn.setAttribute("type","button");
		btn.setAttribute("id","b"+iVal3);
		btn.setAttribute("class","mui-btn mui-btn-blue");
		btn.setAttribute("onclick","OverQu('"+iVal3+"')");
		btn.innerHTML="结束问答";
		li.appendChild(btn);
	}
	else
	{
		var p2 = document.createElement("p")
		p2.setAttribute("style","color:red;");
		p2.setAttribute("id", iVal3);
		p2.innerHTML = "已经结束问答";
		li.appendChild(p2);
	}
	
	return li;
}
function OverQu(id)
{
	mui.confirm('请问是否结束问答？问答一旦结束就不再能重新开始哦！','提示',['取消','确认'],function (e) {
		if(e.index==1)
		{
			var tdata = {"qid":id};
			mui.ajax(ASYURI+'M201012.do',{
				data:tdata,
				dataType:'json',
				type:'post',
				timeout:10000,
				success:function(data){
					if(data.tag=='1')
					{
						mui.alert('因为未知原因结束作答失败！','提示','确认',function (e) { },'div');
					}
					else if(data.tag=='0')
					{
						var p = ID(id);
						p.innerHTML="已经结束回答";
						p.setAttribute("style","color:red;");
						var btn = ID('b'+id);
						btn.setAttribute("disabled","disabled");
						mui.alert('成功结束作答','提示','确认',function (e) {},'div');
					}
				},
				error:function(xhr,type,errorThrown){
					
				}
			});
		}
	},'div')
}
function QuId(id)
{
	mui.confirm('请问是否查看问卷的回答?','提示',['取消','确认'],function (e) {
		if(e.index==1)
		{
			localStorage.setItem("m303qid",id);
			localStorage.setItem("m303title","问卷回答");
			mui.openWindow("m3031.html");
		}
	},'div')
}

function cardInit(cVal1,cVal2)
{
	var card = document.createElement("div");
	card.setAttribute("class","mui-card");
	var cardHead = threeValInit("div","mui-card-header",cVal1);
	cardHead.appendChild(btnInit("head"));
	card.appendChild(cardHead);
	var cardFoot = threeValInit("div","mui-card-footer",cVal2);
	cardFoot.appendChild(btnInit("foot"));
	card.appendChild(cardFoot);
	mr.appendChild(card);
}
function threeValInit(t1,t2,t3)
{
	var it = document.createElement(t1);
	it.setAttribute("class",t2);
	it.innerHTML = t3;
	return it;
}
function btnInit(bVal)
{
	var btn = document.createElement("button");
	btn.setAttribute("type","type");
	btn.setAttribute("class","mui-btn mui-btn-green");
	btn.setAttribute("onclick","onefix('"+bVal+"')");
	btn.innerHTML="修改";
	return btn;
}
function onefix(type)
{
	var hintInfo,tdata={'tid':tid,'type':type};
	if(type=='head'){
		hintInfo = "请输入新的团体名";
	}
	else{
		hintInfo = "请输入新的团体介绍";
	}
		
	mui.prompt(hintInfo,'','提示',['取消','确认修改'],function (e) {
	    if(e.index==1)
		{
			tdata['newinfo']=e.value;
			mui.ajax(ASYURI+'m201013.do',{
				data:tdata,
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					if(data.tag=='1')
					mui.alert('因为未知原因修改失败','提示','确认',function (e) { },'div');
					else if(data.tag=='0')
					{
						var mc = CN('mui-card-'+type+'er');
						mc[0].innerHTML = e.value;
						mui.alert('修改成功','提示','确认',function (e) { },'div');
					}
				},
				error:function(xhr,type,errorThrown){
					
				}
			});
		}
	},'div')
}


m201011Init();